package org.jaronsource.framework.plugins.security;

import java.util.Collection;

import org.jaronsource.framework.plugins.security.domain.IPerm;
import org.jaronsource.framework.plugins.security.domain.SecurityToken;
import org.jaronsource.framework.util.StringUtils;

public class SecurityUtils {

	public static boolean hasURIPerm( SecurityToken token, String requestURI ) {

		if ( StringUtils.isBlank( requestURI ) ) return true;

		Collection<IPerm> perms = token.getPerms();

		for ( IPerm perm : perms ) {
			if ( requestURI.startsWith( perm.getUrl() ) ) return true;
		}

		return false;
	}

	public static boolean hasPerm( SecurityToken token, String permCode ) {

		if ( StringUtils.isBlank( permCode ) ) return true;

		Collection<IPerm> perms = token.getPerms();

		for ( IPerm perm : perms ) {
			if ( permCode.equals( perm.getCode() ) ) return true;
		}

		return false;
	}
}
